<template>
  <div class="app-container">
    <div class="filterDiv">
      <label class="label">截止日期</label>
      <el-date-picker v-model="billDate2" :editable="false" type="date" placeholder="截止日期" size="small" :clearable="false" value-format="yyyy-MM-dd" />
      <el-button v-permiss="['346010']" size="small" type="primary" round @click="getDatavoucher">查看单据数量</el-button>
      <el-button v-permiss="['346020']" size="small" type="primary" style="float:right;margin-top:5px" round @click="queryLog">查看日志</el-button>
      <el-button v-permiss="['346010']" size="small" type="primary" style="float:right;margin-top:5px" round @click="createvoucher">一键生成</el-button>
    </div>
    <div class="contentDiv clearfix">
      <el-table :data="dataList" border fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" />
        <el-table-column label="序号" type="index" align="center" />
        </el-table-column>
        <el-table-column label="业务代码" width="140" align="center">
          <template slot-scope="{row}">
            <span>{{ row.code }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单据名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="生成方式" min-width="140" align="center">
          <template slot-scope="{row}">
            <el-radio-group v-model="row.type">
              <el-radio :label="0" style="margin-right:10px">不生成</el-radio> 
			  <el-radio v-if="row.param === 'isSales'" :label="1">汇总生成-开票</el-radio>
              <el-radio v-else :label="1">汇总生成</el-radio>
              <el-radio v-if="row.param === 'isSales'" :label="2">汇总生成-全部</el-radio>
              <el-radio v-else :label="2">每单生成</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column label="未生成单据数量" align="center">
          <template slot-scope="{row}">
            <span>{{ row.nonum }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{row}">
            <span class="ctrl" @click="createV(row)">生成凭证</span>
            <span class="ctrl" @click="saveV(row)">检测</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { buildVoucher, getCountNotGeneratedVoucherByBillDate, voucherLogList, buildVoucherByCode } from '@/api/voucher'
export default {
  name: 'CreateVoucher',
  data() {
    return {
      billDate2: '',
      dataList: [
        { name: '进货单', code: 'PO01', param: 'isPurchase', type: null },
        { name: '采购退货单', code: 'PO02', param: 'isPurchaseReturned', type: null },
        { name: '销售(订)单', code: 'SO01', param: 'isSales', type: null },
        { name: '销售退货单', code: 'SO02', param: 'isSalesReturned', type: null },
        { name: '预收单', code: 'PS01', param: 'isPresale', type: null },
        { name: '预收退款单', code: 'PS02', param: 'isPresaleReturned', type: null },
        { name: '收款单', code: 'RP01', param: 'isReceipt', type: null },
        { name: '付款单', code: 'RP02', param: 'isPayment', type: null },
        { name: '盘损单', code: 'IL01', param: 'isInventory', type: null }
      ]
    }
  },
  created() {
    this.billDate2 = this.getYestory(-1, '-')
    this.getDatavoucher()
    this.getBillType()
  },
  methods: {
    saveV(row) {
      const obj = {
        billDate2: this.billDate2,
        bizTypeCode: row.code,
        isTemp: 1
      }
      obj[row.param] = row.type
      buildVoucherByCode(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    getBillType() {
      const userInfo = JSON.parse(sessionStorage.userInfo)
      this.dataList.forEach(it => {
        this.$set(it, 'type', userInfo.book[it.param])
      })
    },
    getYestory(num, str) {
      var today = new Date()
      var nowTime = today.getTime()
      var ms = 24 * 3600 * 1000 * num
      today.setTime(parseInt(nowTime + ms))
      var oYear = today.getFullYear()
      var oMoth = (today.getMonth() + 1).toString()
      if (oMoth.length <= 1) oMoth = '0' + oMoth
      var oDay = today.getDate().toString()
      if (oDay.length <= 1) oDay = '0' + oDay
      return oYear + str + oMoth + str + oDay
    },
    getDatavoucher() {
      const obj = { billDate2: this.billDate2 }
      getCountNotGeneratedVoucherByBillDate(obj).then(res => {
        if (res.data.errorCode === '0') {
          const dataObj = res.data.data
          this.dataList.forEach(it => {
            this.$set(it, 'nonum', dataObj[it.code])
          })
        }
      })
    },
    createV(row) {
      const obj = {
        billDate2: this.billDate2,
        bizTypeCode: row.code
      }
      obj[row.param] = row.type
      buildVoucherByCode(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    createvoucher() {
      const obj = {}
      this.dataList.forEach(it => {
        obj[it.param] = it.type
      })
      obj.billDate2 = this.billDate2
      buildVoucher(obj).then(res => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    queryLog() {
      const obj = { billDate2: this.billDate2 }
      console.log(obj)
      voucherLogList(obj).then(res => {
        this.$message.success(res.data.msg)
      })
    }
  }
}
</script>
<style scoped>
.label {
    font-size: 14px;
    margin-right: 5px;
    color: #333;
    margin-left: 10px
}

.label:first-child {
    margin-left: 0
}

.label-item {
    margin-right: 15px;
    float: left;
    font-size: 14px;
}
</style>
